/**
 * Created by yanlin on 2017/11/30.
 */
Ext.onReady(function () {
    Ext.QuickTips.init();
    var formula_grid_row;

    //reader用于解析bxdj_store的内容
    var formula_reader = new Ext.data.JsonReader({
        totalProperty: 'totalProperty',
        root: 'root'
    }, [
        {
            name: 'param_id'
        }, {
            name: 'param_nm'
        },{
            name: 'data_source_id'
        }, {
            name: 'dyzd'
        },{
            name: 'data_source'
        }
    ]);

    //formula_store加载内容
    var formula_store = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/index/formula/all.action'
        }),
        reader: formula_reader
    });

    var dataSource_message = new Ext.data.JsonStore({
        url: '/index/dataSource/combo.action',
        fields: ['value', 'text']
    });
    dataSource_message.load();
    var dataSource_combo_add = new Ext.form.ComboBox({
        name: 'data_source',
        fieldLabel: '数据源',
        emptyText: '请选择',
        mode: 'local',
        editable: true,
        triggerAction: 'all',
        store: dataSource_message,
        width: 150,
        valueField: 'value', // 值
        displayField: 'text',
        hiddenName: 'data_source_id',
        allowBlank: false,
        msgTarget: "side",
        queryParam: 'userinput',
        forceSelection: true,
        listeners: {
            beforequery: function (qe) {
                var para = qe.query;
            }
        }
    });
    var dataSource_combo_update = new Ext.form.ComboBox({
        name: 'data_source',
        fieldLabel: '数据源',
        emptyText: '请选择',
        mode: 'local',
        editable: true,
        triggerAction: 'all',
        store: dataSource_message,
        width: 150,
        valueField: 'value', // 值
        displayField: 'text',
        hiddenName: 'data_source_id',
        allowBlank: false,
        msgTarget: "side",
        queryParam: 'userinput',
        forceSelection: true,
        listeners: {
            beforequery: function (qe) {
                var para = qe.query;
            }
        }
    });

    var formula_form = new Ext.form.FormPanel({
        labelAlign: 'right',
        frame: true,
        labelWidth: 150,
        url: '/index/formula/uppt.action',
        bodyStyle: 'padding:30px',
        reader: formula_reader,
        items: [
            {
                xtype: 'hidden',
//                 fieldLabel: '参数编码',
// //                readOnly: true,
// //                disabled :true,
//                 width: 150,
                name: 'param_id',
                // allowBlank:false,
                // msgTarget:"side"
            },
            {
                xtype: 'textfield',
                fieldLabel: '参数名称',
                width: 150,
                name: 'param_nm'
            },
            dataSource_combo_update,
            {
                xtype: 'textfield',
                fieldLabel: '对应字段',
                width: 150,
                name: 'dyzd'
            }
        ]
    });

    var formula_win = new Ext.Window({
        layout: 'fit',
        title: '公式参数信息修改',
        width: '500',
        height: '300',
        closeAction: 'hide',
        constrain: true,
        buttonAlign: 'center',
        resizable: false,
        modal: true,
        plain: true,

        items: [formula_form],
        buttons: [
            {
                text: '修改',
                handler: function () {
                    if(formula_form.getForm().isValid()){
                        formula_form.getForm().submit({
                            success: function (form, action) {
                                Ext.Msg.alert('提示',
                                    '提交成功！');
                                formula_form.getForm().reset();
                                formula_win.hide();
                                formula_store.reload({
                                    params: {
                                        start : 0,
                                        limit: 30
                                    }
                                });
                            },
                            failure: function () {
                                Ext.Msg.alert('提示',
                                    '提交失败！');
                            }

                        });
                    }

                }},
            {
                text: '取消',
                handler: function () {
                    formula_win.hide();
                }
            }
        ]
    });
    formula_win.on('show', function () {
        var rec = formula_store.getAt(formula_grid_row);
//        alert(rec.get("ucode"));
        formula_form.getForm().loadRecord(rec);
    });

    //表格的列头
    var formula_colModel = new Ext.grid.ColumnModel([
        new Ext.grid.RowNumberer({
            header: '编号',
            width: 35
        }),
        // {
        //     header: "参数编码",
        //     sortable: true,
        //     dataIndex: 'param_id',
        //     width: 370
        // },
        {
            header: "参数名称",
            sortable: true,
            dataIndex: 'param_nm',
            width: 300
        }, {
            header: "数据源",
            sortable: true,
            dataIndex: 'data_source',
            width: 300
        }, {
            header: "对应字段",
            sortable: true,
            dataIndex: 'dyzd',
            width: 300
        },{
            xtype: 'actioncolumn',
            header: "操作",
            items: [
                {
                    icon: '/ext/cog_edit.png',  // Use a URL in the icon config
                    tooltip: '修改',
                    handler: function (grid, rowIndex, colIndex) {
                        var rec = formula_store.getAt(rowIndex);
                        formula_grid_row = rowIndex;
                        formula_win.show();
                    }
                },
                {
                    icon: '/ext/delete.gif',  // Use a URL in the icon config
                    tooltip: '删除',
                    handler: function (grid, rowIndex, colIndex) {
                        var rec = formula_store.getAt(rowIndex);
                        Ext.MessageBox.confirm('删除', '确认删除？', function (btn) {
                            if (btn == 'yes') {
                                Ext.Ajax.request({
                                    url: '/index/formula/dele.action',
                                    params: {
                                        param_id: rec.get('param_id')
                                    },
                                    success: function (response) {
                                        Ext.Msg.alert('提示',
                                            '删除成功！');
                                        formula_store.load({
                                            params: {
                                                start : 0,
                                                limit: 30
                                            }
                                        });

                                    },
                                    failure: function (response) {
                                        Ext.Msg.alert('提示',
                                            '删除失败！');
                                    }

                                });

                            }

                        });
                    }
                }

            ]
        }

    ]);

    var formula_addform = new Ext.form.FormPanel({
        labelAlign: 'right',
        frame: true,
        labelWidth: 150,
        url: '/index/formula/add.action',
        bodyStyle: 'padding:30px',
        reader: formula_reader,
        items: [
//             {
//                 xtype: 'textfield',
//                 fieldLabel: '参数编码',
// //                readOnly: true,
// //                disabled :true,
//                 width: 150,
//                 name: 'param_id',
//                 allowBlank:false,
//                 msgTarget:"side"
//             },

            {
                xtype: 'textfield',
                fieldLabel: '参数名称',
                width: 150,
                name: 'param_nm',
                allowBlank:false
                //msgTarget:"side"
            },
            dataSource_combo_add,
            {
                xtype: 'textfield',
                fieldLabel: '对应字段',
                width: 150,
                name: 'dyzd'
            }
        ]
    });

    var formula_addwin = new Ext.Window({
        layout: 'fit',
        title: '公式参数信息增加',
        width: '500',
        height: '300',
        closeAction: 'hide',
        constrain: true,
        buttonAlign: 'center',
        resizable: false,
        modal: true,
        plain: true,

        items: [formula_addform],
        buttons: [
            {
                text: '增加',
                handler: function () {
                    if(formula_addform.getForm().isValid()){
                        formula_addform.getForm().submit({
                            success: function (form, action) {
                                Ext.Msg.alert('提示',
                                    '提交成功！');
                                formula_addform.getForm().reset();
                                formula_addwin.hide();
                                formula_store.reload({
                                    params: {
                                        start : 0,
                                        limit: 30
                                    }
                                });
                            },
                            failure: function (form, action) {
                                Ext.Msg.alert('提示',
                                    '提交失败！');
                            }

                        });
                    }

                }},
            {
                text: '取消',
                handler: function () {
                    formula_addwin.hide();
                }
            }
        ]
    });

    var formula_grid = new Ext.grid.GridPanel({
        title: "公式参数信息管理",
        width: 1100,
        height: 670,
        renderTo: 'formula',
        cm: formula_colModel,
        collapsible: true,
        autoExpandColumn: 2,
        stripeRows: true,
        loadMask: true,
        store: formula_store,
        tbar: new Ext.Toolbar(['->',
            {
                icon: '/ext/add.gif',
                tooltip:'增加',
                handler: function () {
                    formula_addwin.show();
                }
            }
        ]),
        bbar: new Ext.PagingToolbar({
            firstText: '1',
            pageSize: 30,
            store: formula_store,
            displayInfo: true,
            displayMsg: '显示第 {0} 到 {1}条记录 ，共 {2}条',
            emptyMsg: "没有记录"
        }),
        viewConfig: {
            forceFit: true
        }

    });
    formula_store.load({
        params: {
            start :0,
            limit:30
        }
    });

});